<template>
  <view class="n10-container">
    <view class="count-down">
      <u-count-down
        :time="time * 60 * 1000"
        format="HH:mm:ss"
        @finish="doEnd"
      ></u-count-down>
    </view>
    <view class="top-record" v-if="topRecord > 0"
      >当前最高正确记录为：{{ topRecord }}</view
    >
    <view class="top-record" v-else>当前暂无记录</view>
    <view class="input-box">
      <span>{{ n1 }}</span>
      <span class="ca">{{ ca }}</span>
      <span>{{ n2 }}</span>
      <span class="equal">=</span>
      <span class="res">{{ res }}</span>
    </view>

    <view class="res-box">
      <view class="res-icon-box">
        <u-icon
          v-if="isRight"
          name="checkmark"
          color="green"
          size="62"
        ></u-icon>
        <u-icon v-if="isWrong" name="close" color="red" size="36"></u-icon>
      </view>
      <view class="res-txt-box">
        <view class="row"
          >正确：<span class="rights t1">{{ rights }}</span></view
        >
        <view class="row"
          >错误：<span class="wrongs t1">{{ wrongs }}</span></view
        >
        <view class="row"
          >总共：<span class="t1">{{ total }}</span></view
        >
      </view>
      <view class="btn-list">
        <u-button
          class="btn"
          v-for="n in btnNums"
          :key="n"
          :disabled="isEnd"
          @click="doRes(n)"
          >{{ n }}</u-button
        >
        <u-button class="btn" type="warning" @click="doClear">清空</u-button>
      </view>
      <view class="sub">
        <u-button @click="doSub" :disabled="isEnd" type="primary" size="large"
          >确定</u-button
        >
      </view>
    </view>
  </view>
</template>

<script>
import apis from "../../apis/apis";

const num = 20;
export default {
  data() {
    return {
      time: 5,
      rewardNum: 40,
      n1: "",
      n2: "",
      ca: "",
      res: "",
      btnNums: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
      rights: 0,
      wrongs: 0,
      total: 0,
      isEnd: false,
      isRight: false,
      isWrong: false,
      topRecord: 0,
    };
  },
  created() {
    this.initNumbers();
    this.getRecord();
  },
  methods: {
    async getRecord() {
      this.$http.get(apis.getTopRecord, { rtype: "n20" }).then((res) => {
        console.log("res n20:", res);
        this.topRecord = res.record;
      });
    },
    initNumbers() {
      ~~(Math.random() * num) % 2 ? this.initPlus() : this.initSub();
    },
    initPlus() {
      this.n1 = ~~(Math.random() * num);
      this.n2 = ~~(Math.random() * (num - this.n1));
      this.ca = "+";
    },
    initSub() {
      this.n1 = ~~(Math.random() * num);
      this.n2 = ~~(Math.random() * this.n1);
      this.ca = "-";
    },
    doRes(n) {
      console.log("doRes.n:", n);
      if (this.res.length <= 1) {
        this.res += n + "";
      }
      // const res = this.ca == '+' ? this.n1 + this.n2 : this.n1 - this.n2
      // if (this.res == res) {
      //     this.doSub()
      // }
    },
    doClear() {
      this.res = "";
    },
    doSub() {
      if (this.res == "") {
        return;
      }
      const res = this.ca == "+" ? this.n1 + this.n2 : this.n1 - this.n2;
      console.log("res:", res);
      // res == this.res ? this.rights++ : this.wrongs++
      if (res == this.res) {
        this.rights++;
        this.setRight();
      } else {
        this.wrongs++;
        this.setWrong();
      }
      this.total++;
      const timer = setTimeout(() => {
        this.res = "";
        this.initNumbers();
        clearTimeout(timer);
      }, 200);
    },
    setRight() {
      this.isWrong = false;
      this.isRight = true;
      const timer = setTimeout(() => {
        this.isRight = false;
        clearTimeout(timer);
      }, 1000);
    },
    setWrong() {
      this.isRight = false;
      this.isWrong = true;
      const timer = setTimeout(() => {
        this.isWrong = false;
        clearTimeout(timer);
      }, 1000);
    },
    doEnd() {
      uni.showModal({
        title: "提示",
        content: `恭喜你，在${this.time}分钟内答对了 ${this.rights} 道题目！\n如果${this.time}分钟内答对的题数超过${this.rewardNum}，就可以奖励玩游戏哟！`,
        success: () => {
          this.$http.post(apis.saveRecord, {
            record: this.rights,
            rtype: "n20",
          });
          if (this.rights >= this.rewardNum) {
            this.gotoGame();
          }
        },
      });
      this.isEnd = true;
    },
    gotoGame() {
      console.log("gotoGame");
      uni.navigateToMiniProgram({
        appId: "wxb95bb2876042e715",
        envVersion: "trial",
      });
    },
  },
};
</script>

<style lang="scss">
.n10-container {
  width: 100vw;
  height: 100vh;
  font-size: 32rpx;
  .count-down {
    width: 100%;
    padding: 30rpx 0;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .top-record {
    width: 100%;
    display: flex;
    justify-content: center;
    color: #42b983;
  }
  .input-box {
    width: 600rpx;
    height: 140rpx;
    margin: 20rpx auto;
    border: 6rpx solid #42b983;
    border-radius: 20rpx;
    padding: 10rpx;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    span {
      margin-left: 40rpx;
      // width: 30rpx;
      display: inline-block;
      font-size: 84rpx;
      &.ca {
        color: red;
        // font-size: 104rpx;
      }
      &.res {
        display: inline-block;
        width: 120rpx;
        height: 100rpx;
        border-bottom: 1rpx solid red;
        text-align: center;
      }
    }
  }
  .res-box {
    width: 600rpx;
    margin: 50rpx auto;
    .res-icon-box {
      width: 100%;
      height: 40rpx;
      display: flex;
      justify-content: center;
      padding: 10rpx 0;
    }
    .res-txt-box {
      width: 550rpx;
      margin: 50rpx auto;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .row {
        display: flex;
        align-items: center;
      }
      .t1 {
        font-size: 42rpx;
      }
      .rights {
        color: green;
      }
      .wrongs {
        color: red;
      }
    }
    .btn-list {
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      button {
        width: 30%;
        margin-bottom: 20rpx;
        font-size: 42rpx;
      }
    }
    .sub {
      margin-top: 40rpx;
      button {
        height: 120rpx;
      }
    }
  }
}
</style>
